package com.da.netty.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

public class NettyByteBuf02 {
    public static void main(String[] args) {
        //创建ByteBuf
        ByteBuf byteBuf = Unpooled.copiedBuffer("hello,Joker达少", CharsetUtil.UTF_8);
        if (byteBuf.hasArray()) {
            byte[] content = byteBuf.array();
            //将 content 转成字符串
            System.out.println(new String(content, CharsetUtil.UTF_8));

            System.out.println("byteBuf=" + byteBuf);
            System.out.println(byteBuf.arrayOffset()); // 0
            System.out.println(byteBuf.readerIndex()); // 0
            System.out.println(byteBuf.writerIndex()); // 17
            System.out.println(byteBuf.capacity()); // 39

            // 第一种方法会影响可读字节，第二种不会
            //System.out.println(byteBuf.readByte());
            System.out.println(byteBuf.getByte(0));

            System.out.println(byteBuf.readableBytes());//可读的字节数  17

            //使用for取出各个字节
            for (int i = 0; i < byteBuf.readableBytes(); i++) {
                System.out.println((char)byteBuf.getByte(i));
            }

            //按照某个范围读取
            System.out.println(byteBuf.getCharSequence(0, 5, CharsetUtil.UTF_8));
            System.out.println(byteBuf.getCharSequence(11, 6, CharsetUtil.UTF_8));
        }
        System.out.println("执行完毕");
    }
}